package com.ewhale.feitengguest.ui.shop.task

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.dto.TabEntity
import com.ewhale.feitengguest.dto.WaitHandleDto
import com.ewhale.feitengguest.presenter.shop.task.SubmitListPresenter
import com.ewhale.feitengguest.ui.shop.deal.HandlerTaskActivity
import com.ewhale.feitengguest.ui.shop.deal.adapter.DealAdapter
import com.ewhale.feitengguest.view.shop.task.SubmitListView
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.simga.library.activity.MBaseActivity
import kotlinx.android.synthetic.main.activity_submit_list.*
import kotlinx.android.synthetic.main.activity_submit_list.listview
import kotlinx.android.synthetic.main.activity_submit_list.ref_layout
import kotlinx.android.synthetic.main.activity_submit_list.xloading_view
import java.util.ArrayList

class SubmitListActivity : MBaseActivity<SubmitListPresenter>(), SubmitListView {

    private var missionId: Long? = null
    private val mTitles = arrayOf("待处理", "已完成", "不及格")
    private val mTabEntities = ArrayList<CustomTabEntity>()
    private val mAdapter by lazy(LazyThreadSafetyMode.NONE) { DealAdapter() }
    private var pageNumber = 1
    private var status = 1
    private val myReceiver by lazy(LazyThreadSafetyMode.NONE) { MyReciver() }

    override fun getViewId(): Int {
        return R.layout.activity_submit_list
    }

    override fun isUseBaseTitleBar(): Boolean {
        return true
    }

    override fun init(savedInstanceState: Bundle?) {
        setTitle("提交信息")

        val filter = IntentFilter()
        filter.addAction("UPDATE_SUBMIT_LIST")
        registerReceiver(myReceiver, filter)

        for (i in mTitles.indices) {
            mTabEntities.add(TabEntity(mTitles[i], 0, 0))
        }
        acty_main_tab.setTabData(mTabEntities)

        listview.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
        }
        xloading_view.showLoading()
        presenter.loadSubmitList(missionId!!, status, pageNumber)
    }

    override fun showDealList(list: List<WaitHandleDto>) {
        if (pageNumber == 1) {
            mAdapter.setNewData(list)
        } else {
            mAdapter.addData(list)
        }
        if (mAdapter.mData.size == 0) {
            xloading_view.showEmpty()
        } else {
            xloading_view.showContent()
        }
        onLoad(list.size)
    }

    override fun initListener() {
        super.initListener()
        acty_main_tab.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                pageNumber = 1
                when (position) {
                    0 -> status = 1
                    1 -> status = 3
                    2 -> status = 2
                }
                presenter.loadSubmitList(missionId!!, status, pageNumber)
            }

            override fun onTabReselect(position: Int) {}
        })
        ref_layout.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                pageNumber++
                presenter.loadSubmitList(missionId!!, status, pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                pageNumber = 1
                presenter.loadSubmitList(missionId!!, status, pageNumber)
            }
        })
        mAdapter.setOnItemClickListener { _, item, _ ->
            HandlerTaskActivity.open(mContext, item.id)
        }
        xloading_view.setOnRetryClickListener {
            pageNumber = 1
            xloading_view.showLoading()
            presenter.loadSubmitList(missionId!!, status, pageNumber)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(myReceiver)
    }

    inner class MyReciver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent!!.action == "UPDATE_SUBMIT_LIST") {
                pageNumber = 1
                presenter.loadSubmitList(missionId!!, status, pageNumber)
            }
        }
    }

    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String, message: String) {
        showErrorMsg(code, message)
        onLoad(-1)
        if (postCode == 1 || postCode == -10086) {
            xloading_view.showError()
        }
    }

    private fun onLoad(size: Int) {
        if (ref_layout == null) {
            return
        }
        ref_layout.finishRefreshLoadingMore()
        if (size < 10) {
            ref_layout.enableLoadMore(false)
        } else {
            ref_layout.enableLoadMore(true)
        }
    }

    companion object {
        fun open(mContext: MBaseActivity<*>, missionId: Long) {
            val bundle = Bundle()
            bundle.putLong("missionId", missionId)
            mContext.startActivity(bundle, SubmitListActivity::class.java)
        }
    }

    override fun onGetBundle(bundle: Bundle) {
        missionId = bundle.getLong("missionId")
    }

}
